import Mock from "mockjs";
import { param2Obj } from "../utils/mUtils";

let List = [];
for (var i = 0; i < 200; i++) {
  List.push({
    id: Mock.mock("@id"),
    class: "课程订单",
    name: "幼儿英语课程",
    price: 2366,
    state: Mock.mock("@integer(0, 1)"), //0待付款 1已付款
    date: Mock.mock("@datetime"),
  });
}

export default {
  getfinanceData: (config) => {
    let { search, page, pageSize, value1, value2 } = param2Obj(config.url);
    // 搜素l
    value1 = parseInt(value1); //将字符串转成数值型
    value2 = parseInt(value2); //将字符串转成数值型

    let newList = List.filter((item) => {
      // console.log(Date.parse(item.date));
      //判断搜索符合的内容
      return item.id.indexOf(search) != -1;
    });

    //日期筛选逻辑
    let newList2 = []; //创建一个空数组用来存放
    if (!isNaN(value1) && !isNaN(value2)) {
      //判断日期上传的value值有没有，如果有做if里的日期筛选操作
      let timeList = newList.filter((item) => {
        let k = new Date(item.date).getTime(); //将每个订单的日期赋值为k，判断k是否在这个时间段内
        return value1 <= k && k <= value2;
      });
      newList2 = timeList.filter((item, index) => {
        return (page - 1) * pageSize <= index && index < page * pageSize; //将筛选出来符合日期的数据做分页
      });

      return {
        //返回if里的retun，上下二选一
        code: 200,
        msg: "请求成功",
        data: newList2,
        total: timeList.length,
      };
    } else {
      //如果没有选择日期则做以下流程
      //分页逻辑
      newList2 = newList.filter((item, index) => {
        return (page - 1) * pageSize <= index && index < page * pageSize;
      });

      return {
        //返回else里的retun，上下二选一
        code: 200,
        msg: "请求成功",
        data: newList2,
        total: newList.length,
      };
    }
  },

  //批量删除
  batchDeleteData: (config) => {
    let { ids } = JSON.parse(config.body);
    let newList = List.filter((item) => {
      return ids.indexOf(item.id) === -1;
    });
    List = newList;
    return {
      code: 200,
      msg: "删除成功",
      data: null,
    };
  },
};

//重置按钮逻辑
